package com.lv.controller;

import com.lv.data.Result;
import com.lv.pojo.TbContent;
import com.lv.service.ContentService;
import com.lv.service.CotegoryService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/content")
public class ContentController {

    @DubboReference
    private ContentService cs;

    @DubboReference
    private CotegoryService gs;

    @RequestMapping("toshow")
    public String toshow(Model m) {
        m.addAttribute("contentCatList",gs.getAll(null));
        return "admin/content";
    }

    //查询
    @RequestMapping("getAll")
    @ResponseBody
    public Result getInfo() {
        try {
            List<TbContent> list= cs.getInfo();
            return Result.OK().setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ER();
        }
    }
    @RequestMapping("insert")
    @ResponseBody
    public Result insert(TbContent t){
        try {
            cs.insert(t);
            return Result.OK();
        } catch (Exception e) {
            return Result.ER();
        }
    }

    @RequestMapping("getById")
    @ResponseBody
    public Result getById(Long id,Model m){
        try {
            TbContent byId = cs.getById(id);
            m.addAttribute("c",byId);
            return Result.OK().setData(byId);
        } catch (Exception e) {
            return Result.ER();
        }
    }

    @RequestMapping("update")
    @ResponseBody
    public Result update(TbContent t){
        try {
            cs.update(t);
            return Result.OK();
        } catch (Exception e) {
            return Result.ER();
        }
    }

    @RequestMapping("deleteCheck")
    @ResponseBody
    public Result deleteCheck(Long[] ids){
        try {
            cs.deleteCheck(ids);
            return Result.OK();
        } catch (Exception e) {
            return Result.ER();
        }
    }
}
